//
//  ForgotPasswordViewController.m
//  suRead
//
//  Created by 师爱华 on 17/3/2.
//  Copyright © 2017年 bao. All rights reserved.
//

#import "ForgotPasswordViewController.h"
#import "UIControl+Blocks.h"
#import "UIViewController+HUD.h"

#import "ServiceUserNew.h"

@interface ForgotPasswordViewController ()<UIGestureRecognizerDelegate>

@property (strong, nonatomic) IBOutlet UINavigationItem *forgotNavigationItem;
@property (strong, nonatomic) IBOutlet UIView *forgotAccountView;
@property (strong, nonatomic) IBOutlet UITextField *mobileText;
@property (strong, nonatomic) IBOutlet UITextField *validCodeText;
@property (strong, nonatomic) IBOutlet UITextField *passwordText;
@property (strong, nonatomic) IBOutlet UITextField *password2Text;
@property (strong, nonatomic) IBOutlet UIButton *checkCodeButton;
@property (strong, nonatomic) IBOutlet UIButton *commitButton;

@end

@implementation ForgotPasswordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
//    __weak typeof(self) weak_self = self;
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    // enable slide-back
    if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        self.navigationController.interactivePopGestureRecognizer.enabled = YES;
        self.navigationController.interactivePopGestureRecognizer.delegate = self;
    }
    
    self.navigationController.navigationBar.backgroundColor = [UIColor whiteColor];
    self.view.backgroundColor = [UIColor colorWithRed:238/255.0f green:238/255.0f blue:238/255.0f alpha:1.0f];
    
    
    
//    UIButton *buttonComment = [UIButton buttonWithType:UIButtonTypeCustom];
//    buttonComment.titleLabel.font = [UIFont systemFontOfSize:18.0f];
//    buttonComment.frame = CGRectMake(0, 0, 50.0f, 25.0f);
//    [buttonComment setTitle:@"提交" forState:UIControlStateNormal];
//    [buttonComment setBackgroundColor:[UIColor redColor]];
//    buttonComment.layer.cornerRadius = 10.0f;
//    buttonComment.layer.masksToBounds = YES;
//    UIBarButtonItem *submitItem = [[UIBarButtonItem alloc] initWithCustomView:buttonComment];
//    [buttonComment addActionForControlEvents:UIControlEventTouchUpInside usingBlock:^(UIControl *sender, UIEvent *event) {
//
//    }];
//    self.forgotNavigationItem.rightBarButtonItem = submitItem;
    
    
    self.forgotAccountView.layer.borderColor = [UIColor colorWithRed:233/255.0f green:233/255.0f blue:233/255.0f alpha:1.0f].CGColor;
    self.forgotAccountView.layer.borderWidth = 1.0f;
    

}

- (NSString *)valiMobile:(NSString *)mobile{
    if (mobile.length < 11)
    {
        return @"手机号长度只能是11位";
    }else{
        /**
         * 移动号段正则表达式
         */
        NSString *CM_NUM = @"^((13[4-9])|(147)|(15[0-2,7-9])|(178)|(18[2-4,7-8]))\\d{8}|(1705)\\d{7}$";
        /**
         * 联通号段正则表达式
         */
        NSString *CU_NUM = @"^((13[0-2])|(145)|(15[5-6])|(176)|(18[5,6]))\\d{8}|(1709)\\d{7}$";
        /**
         * 电信号段正则表达式
         */
        NSString *CT_NUM = @"^((133)|(153)|(177)|(18[0,1,9]))\\d{8}$";
        NSPredicate *pred1 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CM_NUM];
        BOOL isMatch1 = [pred1 evaluateWithObject:mobile];
        NSPredicate *pred2 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CU_NUM];
        BOOL isMatch2 = [pred2 evaluateWithObject:mobile];
        NSPredicate *pred3 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CT_NUM];
        BOOL isMatch3 = [pred3 evaluateWithObject:mobile];
        
        if (isMatch1 || isMatch2 || isMatch3) {
            return nil;
        }else{
            return @"请输入正确的电话号码";
        }
    }
    return nil;
}

//用户名
- (BOOL) validateUserName:(NSString *)name
{
    NSString *userNameRegex = @"^[A-Za-z0-9]{6,20}+$";
    NSPredicate *userNamePredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",userNameRegex];
    BOOL B = [userNamePredicate evaluateWithObject:name];
    return B;
}

//验证码
- (BOOL) validateCode:(NSString *)name
{
    NSString *codeRegex = @"^[0-9]{6,20}+$";
    NSPredicate *codePredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",codeRegex];
    BOOL B = [codePredicate evaluateWithObject:name];
    return B;
}


//密码
- (BOOL) validatePassword:(NSString *)passWord
{
    NSString *passWordRegex = @"^[a-zA-Z0-9]{6,20}+$";
    NSPredicate *passWordPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",passWordRegex];
    return [passWordPredicate evaluateWithObject:passWord];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    [self.view endEditing:YES];
}

- (IBAction)goBack:(id)sender
{
    [self.navigationController popViewControllerAnimated:YES];
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
    return YES;
}

- (IBAction)buttonCodeOnclick:(id)sender
{
    if([self.mobileText.text isEqualToString:@""]){
        return [theAppDelegate alert:@"手机号不能为空!"];
    }
    NSString *checkMobile =[self valiMobile:self.mobileText.text];
    if(checkMobile!=nil) {
        return [theAppDelegate alert:@"手机号不正确!"];
    }
   
    __weak typeof(self) weak_self = self;
    [self showHUDProgress:@""];
    [ServiceUserNew doGetPhoneCode:self.mobileText.text success:^(id  _Nonnull responseObject, NSString * _Nonnull message) {
        [weak_self removeHUDProgress];
        [weak_self showHUDText:message];
    } failure:^(NSString * _Nonnull message) {
        [weak_self removeHUDProgress];
        [weak_self showHUDText:message];
    }];
}

- (IBAction)buttonCommitOnclick:(id)sender
{
    if([self.mobileText.text isEqualToString:@""]){
        return [theAppDelegate alert:@"手机号不能为空!"];
    }
    NSString *checkMobile =[self valiMobile:self.mobileText.text];
    if(checkMobile!=nil) {
        return [theAppDelegate alert:@"手机号不正确!"];
    }
    
    if([self.validCodeText.text isEqualToString:@""]){
        return [theAppDelegate alert:@"验证码不能为空!"];
    }
    if (![self validateCode:self.validCodeText.text]) {
        return [theAppDelegate alert:@"验证码不正确!"];
    }
    if([self.passwordText.text isEqualToString:@""] || [self.password2Text.text isEqualToString:@""]){
        return [theAppDelegate alert:@"密码不能为空!"];
    }
    
    if(![self.passwordText.text isEqualToString:self.password2Text.text] ){
        return [theAppDelegate alert:@"两次输入密码不一致!"];
    }
    
    if (![self validatePassword:self.passwordText.text]) {
        return [theAppDelegate alert:@"密码格式不正确!"];
    }
    
    __weak typeof(self) weak_self = self;
    [self showHUDProgress:nil];
    [ServiceUserNew doForgotPassword:self.mobileText.text new_pwd:self.passwordText.text new_pwd2:self.password2Text.text code:self.validCodeText.text success:^(id  _Nonnull responseObject, NSString * _Nonnull message) {
        [weak_self removeHUDProgress];
        [weak_self showHUDText:message];
        [weak_self.navigationController popViewControllerAnimated:YES];
    } failure:^(NSString * _Nonnull message) {
        [weak_self removeHUDProgress];
        [weak_self showHUDText:message];
    }];
}

@end
